## R code for Samara's Analysis 
## Advances Article 
## August 2025 

## Read in the data 
library(tidyverse)
library(haven)
library(labelled)
library(modelsummary)
library(here)
rm(list=ls())

here::i_am("dot_code_figs/fig-7-8.R")

samara <- read_dta(file="Klar Data/Klar Data_TrueDot May 2025.dta")

## get the coding for age and education 
# > val_labels(samara$age4)
# 18-34 35-49 50-64   65+ 
#   1     2     3     4 
# > val_labels(samara$Education)
# I did not complete high school   High school degree or G.E.D.        Some college, no degree 
# 1                              2                              3 
# Associate's (two-year) degree  Bachelor's (four-year) degree            Postgraduate degree 
# 4                              5                              6 

###########################
## Graph: national power ## 
###########################

## 3-point Party ID 
samara$pid3 <- ifelse(samara$PoliticalAffiliation < 4,1,
                      ifelse(samara$PoliticalAffiliation > 4,3,2))
table(samara$pid3,samara$PoliticalAffiliation)
samara$pid3 <- factor(samara$pid3,
                      labels = c("Democrat","Independent","Republican"))

samara_summary <- samara %>%
  filter(!is.na(pid3)) %>%
  group_by(pid3, NationalPower) %>%
  summarise(count = n(), .groups = "drop") %>%
  group_by(pid3) %>%
  mutate(
    total = sum(count),
    percent = 100 * count / total
  )



png(file = "dot_code_figs/figs/figure-7.png",
    height = 6,
    width = 10,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

ggplot(samara_summary, aes(x = NationalPower, y = percent, fill = pid3)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
  geom_text(aes(label = round(percent)), 
            position = position_dodge(width = 0.9), 
            vjust = -0.5) +
  xlab("") +
  ylab("Percentage (%)") +
  ylim(c(0, 60)) +
  scale_fill_manual(
    name = "",
    values = c("black", "#545454", "grey"),
    labels = c("Democrats", "Independents", "Republicans")
  ) +
  scale_x_continuous(
    breaks = c(1:7),
    labels = c(
      "Dems. Hold \n All the Power", "Dems. Hold \n Most Power",
      "Dems. Hold \n Slightly More \n Power", "Both Parties \n Equal Power",
      "Reps. Hold \n Slightly More \n Power", "Reps. Hold \n Most Power",
      "Reps. Hold \n All the Power"
    )
  ) +
  theme_bw() +
  theme(
    legend.position = "bottom",
    panel.grid = element_blank()
  ) +
  labs(caption = "TrueDot (N=1,265)")

dev.off() 

########################
## Graph: state power ##
########################

samara_summary2 <- samara %>%
  filter(!is.na(RedState) & !is.na(StatePower)) %>%
  group_by(RedState, StatePower) %>%
  summarise(count = n(), .groups = "drop") %>%
 ungroup()%>%
  group_by(RedState) %>%
  mutate(
    total = sum(count),
    percent = 100 * count / total
  )

sum(samara_summary2$percent)

png(file = "dot_code_figs/figs/figure-8.png",
    height = 6,
    width = 10,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

ggplot(samara_summary2, aes(x = StatePower, y = percent, fill = as.character(RedState))) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
  geom_text(aes(label = round(percent)), 
            position = position_dodge(width = 0.9), 
            vjust = -0.5) +
  xlab("") +
  ylab("Percentage (%)") +
  ylim(c(0, 60)) +
  scale_fill_manual(
    name = "",
    values = c("black", "grey"),
    labels = c("Democratic Controlled State", "Republican Controlled State")
  ) +
  scale_x_continuous(
    breaks = c(1:7),
    labels = c(
      "Dems. Hold \n All the Power", "Dems. Hold \n Most Power",
      "Dems. Hold \n Slightly More \n Power", "Both Parties \n Equal Power",
      "Reps. Hold \n Slightly More \n Power", "Reps. Hold \n Most Power",
      "Reps. Hold \n All the Power"
    )
  ) +
  theme_bw() +
  theme(
    legend.position = "bottom",
    panel.grid = element_blank()
  ) +
  labs(caption = "TrueDot (N=1,265)")
dev.off()